<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="标记名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入标记名称" clearable @keyup.enter="handleQuery"/>
      </el-form-item>
      <el-form-item label="标记类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择标记类型" clearable style="width: 200px">
          <el-option v-for="dict in marker_type" :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd()" v-hasPermi="['dashboard:marker:add']"
        >新增
        </el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="success"
            plain
            icon="Edit"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['dashboard:marker:edit']"
        >修改
        </el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            icon="Delete"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['dashboard:marker:remove']"
        >删除
        </el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['dashboard:marker:export']"
        >导出
        </el-button
        >
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="markerList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <!-- <el-table-column label="标记ID" align="center" prop="id" /> -->
      <el-table-column label="标记名称" align="center" prop="name"/>
      <el-table-column label="标记背景" align="center" prop="background">
        <template #default="scope">
          <img style="width: 100px; height: 50px" :src="handleBackground(scope.row.background)"/>
        </template>
      </el-table-column>
      <!-- <el-table-column label="标记字体色" align="center" prop="fontColor" /> -->
      <el-table-column label="标记类型" align="center" prop="type">
        <template #default="scope">
          <dict-tag :options="marker_type" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column label="触发内容" align="center" prop="content">
        <template #default="scope">
          <el-button @click="openDetail(scope.$index)" v-if="scope.row.type == 3">查看详情</el-button>
          <span v-else>{{ scope.row.content}}</span>
        </template>
      </el-table-column>
      <el-table-column label="标记位置" align="center" prop="position"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button
              link
              type="primary"
              icon="Edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['dashboard:marker:edit']"
          >修改
          </el-button
          >
          <el-button
              link
              type="primary"
              icon="Delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['dashboard:marker:remove']"
          >删除
          </el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"/>

    <!-- 添加或修改标记管理对话框 -->
    <el-dialog :title="title" v-model="open" width="600px" append-to-body :z-index="100">
      <el-form label-position="top" ref="markerRef" :model="form" :rules="rules">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="标记名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入标记名称"/>
            </el-form-item>
          </el-col>
          <!-- <el-col :span="12">
            <el-form-item label="标记字体色" prop="fontColor">
              <el-input type="color" v-model="form.fontColor" placeholder="请输入标记字体色" />
            </el-form-item>
          </el-col> -->
        </el-row>
        <el-form-item label="标记背景" prop="background">
          <img class="background-img" src="../../assets/images/1.png"/>
          <img class="background-img" src="../../assets/images/2.png"/>
          <img class="background-img" src="../../assets/images/3.png"/>
          <img class="background-img" src="../../assets/images/4.png"/>
          <el-radio-group v-model="form.background">
            <el-radio-button label="深蓝背景" value="1"/>
            <el-radio-button label="黄背景" value="2"/>
            <el-radio-button label="绿背景" value="3"/>
            <el-radio-button label="青蓝背景" value="4"/>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="标记类型" prop="type" label-width="220px">
          <el-select v-model="form.type" placeholder="请选择标记类型">
            <el-option
                v-for="dict in marker_type"
                :key="dict.value"
                :label="dict.label"
                :value="parseInt(dict.value)"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="触发内容" prop="content" v-if="form.type == 1 || form.type == 2 || form.type == 3">
          <image-upload v-if="form.type == 1" v-model="form.content" :limit="1"/>
          <el-input v-if="form.type == 2" v-model="form.content" placeholder="请输入链接"/>
          <editor
              v-if="form.type==3"
              v-model="form.content"
          ></editor>
        </el-form-item>
        <el-form-item label="标记位置" prop="position">
          <el-input :disabled="father" v-model="form.position" placeholder="请输入标记位置"/>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
    <el-dialog title="内容详情" width="500px" v-model="detail">
      <div v-html="markerList[nowIndex].content"></div>
    </el-dialog>
  </div>
</template>

<script setup name="Marker">
import {listMarker, getMarker, delMarker, addMarker, updateMarker} from '@/api/dashboard/marker'

const {proxy} = getCurrentInstance()
const {marker_type} = proxy.useDict('marker_type')
const markerList = ref([])
const open = ref(false)
const detail = ref(false)
const nowIndex = ref(0)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref('')
const father = ref(false)

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: null,
    type: null
  },
  rules: {
    name: [{required: true, message: '标记名称不能为空', trigger: 'blur'}],
    type: [{required: true, message: '标记类型不能为空', trigger: 'change'}],
    position: [{required: true, message: '标记位置不能为空', trigger: 'blur'}],
    content: [{required: true, message: '触发内容不能为空', trigger: 'blur'}]
  }
})

const {queryParams, form, rules} = toRefs(data)

/** 查询标记管理列表 */
function getList() {
  loading.value = true
  listMarker(queryParams.value).then((response) => {
    markerList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  father.value = false
  form.value = {
    id: null,
    name: null,
    background: 1,
    fontColor: null,
    type: null,
    content: null,
    position: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null
  }
  proxy.resetForm('markerRef')
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm('queryRef')
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd(position) {
  reset()
  if (position) {
    form.value.position = `${position.lng},${position.lat}`
    father.value = true
  }
  open.value = true
  title.value = '添加标记管理'
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _id = row.id || ids.value
  getMarker(_id).then((response) => {
    form.value = response.data
    open.value = true
    title.value = '修改标记管理'
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs['markerRef'].validate((valid) => {
    if (valid) {
      if (form.value.id != null) {
        updateMarker(form.value).then((response) => {
          proxy.$modal.msgSuccess('修改成功')
          open.value = false
          getList()
          emit('submit')
        })
      } else {
        addMarker(form.value).then((response) => {
          proxy.$modal.msgSuccess('新增成功')
          open.value = false
          getList()
          emit('submit')
        })
      }
    }
  })
}

const emit = defineEmits(['submit'])

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value
  proxy.$modal
      .confirm('是否确认删除标记管理编号为"' + _ids + '"的数据项？')
      .then(function () {
        return delMarker(_ids)
      })
      .then(() => {
        getList()
        proxy.$modal.msgSuccess('删除成功')
      })
      .then(() => {
        emit('submit')
      })
      .catch(() => {
      })
}

defineExpose({
  handleAdd,
  handleDelete,
  handleUpdate
})

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
      'dashboard/marker/export',
      {
        ...queryParams.value
      },
      `marker_${new Date().getTime()}.xlsx`
  )
}

function handleBackground(background) {
  return `/api/profile/upload/assets/${background}.png`
}

/** 打开详情 */
function openDetail(index) {
  nowIndex.value = index
  detail.value = true
}

getList()
</script>

<style lang="scss">
.el-radio-button__inner,
.background-img {
  width: 116px;
}
</style>
